package com.micro.train.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micro.train.entity.Station;
import com.micro.train.repository.StationRepository;
import com.micro.train.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车站信息服务实现类
 */
@Service
public class StationServiceImpl extends ServiceImpl<StationRepository, Station> implements StationService {
    
    @Autowired
    private StationRepository stationRepository;
    
    @Override
    public Station addStation(Station station) {
        // 检查车站代码是否已存在
        if (getStationByCode(station.getStationCode()) != null) {
            throw new RuntimeException("车站代码已存在：" + station.getStationCode());
        }
        stationRepository.insert(station);
        return station;
    }
    
    @Override
    public Station updateStation(Long stationId, Station station) {
        Station existingStation = getStationById(stationId);
        if (existingStation == null) {
            throw new RuntimeException("车站不存在，ID：" + stationId);
        }
        
        // 如果修改了车站代码，需要检查是否与其他车站冲突
        if (!existingStation.getStationCode().equals(station.getStationCode()) && 
            getStationByCode(station.getStationCode()) != null) {
            throw new RuntimeException("车站代码已存在：" + station.getStationCode());
        }
        
        station.setStationId(stationId);
        stationRepository.updateById(station);
        return station;
    }
    
    @Override
    public void deleteStation(Long stationId) {
        Station station = getStationById(stationId);
        if (station == null) {
            throw new RuntimeException("车站不存在，ID：" + stationId);
        }
        stationRepository.deleteById(stationId);
    }
    
    @Override
    public List<Station> getAllStations() {
        return stationRepository.selectList(null);
    }
    
    @Override
    public Station getStationById(Long stationId) {
        return stationRepository.selectById(stationId);
    }
    
    @Override
    public Station getStationByCode(String stationCode) {
        return stationRepository.selectByStationCode(stationCode);
    }
    
    @Override
    public List<Station> getStationsByCityCode(String cityCode) {
        return stationRepository.selectByCityCode(cityCode);
    }
    
    @Override
    public List<Station> getStationsByCityName(String cityName) {
        return stationRepository.selectByCityName(cityName);
    }
    
    @Override
    public List<Station> getStationsByProvince(String province) {
        return stationRepository.selectByProvince(province);
    }
    
    @Override
    public List<Station> getStationsByStatus(Integer status) {
        return stationRepository.selectByStatus(status);
    }
    
    @Override
    public List<Station> getStationsByNameLike(String stationName) {
        return stationRepository.selectByStationNameLike(stationName);
    }
}
